package com.lovefreedom.psi.dao.impl;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.lovefreedom.psi.bean.UserPermission;
import com.lovefreedom.psi.dao.UserPermissionDao;
import com.lovefreedom.psi.util.HibernateSessionFactory;

public class UserPermissionDaoImpl implements UserPermissionDao {
	@Override
	public void addUserPermission(UserPermission userPermission) {
		Session s = null;
		Transaction tx = null;
		try{
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();
			s.save(userPermission);
			tx.commit();
		}finally{
			if(s!=null)
				s.close();
		}
	}

	@Override
	public void updateUserPermission(UserPermission userPermission) {

		Session s = null;
		Transaction tx = null;
		try{
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();
			s.update(userPermission);
			tx.commit();
		}finally{
			if(s!=null)
				s.close();
		}
	}

	@Override
	public void deleteUserPermission(UserPermission userPermission) {

		Session s = null;
		Transaction tx = null;
		try{
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();
			s.delete(userPermission);
			tx.commit();
		}finally{
			if(s!=null)
				s.close();
		}
	}

	@Override
	public UserPermission findUserPermissionById(int id) {	

		Session s = null;
		try{
			s = HibernateSessionFactory.getSession();
			UserPermission userPermission = (UserPermission)s.get(UserPermission.class,id);
			return userPermission;
		}finally{
			if(s!=null)
				s.close();
		}
	}
}
